package cn.gson.school.controller.qiantai;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;

import cn.gson.school.dao.qiantai.ProjectDataDao;
import cn.gson.school.entity.jiaoxue.HonorEntity;
import cn.gson.school.entity.jiaoxue.ProjectDataEntity;
import cn.gson.school.entity.jiaoxue.TeamEntity;
import cn.gson.school.entity.xitong.ImageEntity;
import cn.gson.school.entity.xitong.UserEntity;
import cn.gson.school.mappers.qiantai.ProjectTeamMapper;
import cn.gson.school.service.ImagesService;

/**
 * 前台计划Controller
 */
@Controller
public class ProjectTeamController {
	@Autowired
	private ProjectTeamMapper teamMapper;

	@Autowired
	private ProjectDataDao dataDao;
	
	@Autowired
	private ImagesService imgService;
	
	/**
	 * 查询我的团队列表
	 * 
	 * @param model
	 * @param request
	 * @return
	 * @throws ParseException
	 */
	@GetMapping("qt-team-list")
	public String form(Model model, HttpServletRequest request) throws ParseException {
		Long id = Long.parseLong(request.getParameter("id"));
		if (id != null) {
			List<Map<String, Object>> list = teamMapper.listTeam(id);
			System.out.println("df"+list);
			if (list != null) {
				System.out.println("团队列表：" + list);
				model.addAttribute("teamlist", list);
				for(int i = 0; i < list.size(); i++) {
					List<Map<String, Object>> teamMembers = teamMapper.teamMembers(Long.valueOf(String.valueOf(list.get(i).get("team_id"))));
					//System.out.println("团队成员：" + teamMembers);
					model.addAttribute("teamMembers", teamMembers);
				}
			}
		}
		return "qt-project-team";
	}
	/**
	 * 我的最佳团队列表
	 * @param model
	 * @param request
	 * @return
	 * @throws ParseException
	 */
	@GetMapping("qt-team-win-list")
	public String formWin(Model model, HttpServletRequest request) throws ParseException {
		Long id = Long.parseLong(request.getParameter("id"));
		if (id != null) {
			List<HonorEntity> list = teamMapper.listBestTeam(id);
			if (list != null) {
				System.out.println("最佳团队列表：" + list);
				model.addAttribute("honorlist", list);
			}
		}
		return "qt-project-win";
	}
	
	/**
	 * 项目资料列表
	 */
	@GetMapping("qt-data-list")
	public String formData(Model model, HttpServletRequest request) throws ParseException {
		Long id = Long.parseLong(request.getParameter("id"));
		if (id != null) {
			List<TeamEntity> list = teamMapper.listData(id);
			if (list != null) {
				System.out.println("项目资料列表：" + list);
				model.addAttribute("datalist", list);
			}else {
				model.addAttribute("error", "暂无资料");
			}
		}
		return "qt-project-data";
	}
	/**
	 * 删除资料
	 */
	@GetMapping("qt-data-delete")
	public String dateleData(Model model, HttpServletRequest request) throws ParseException {
		Long id = Long.parseLong(request.getParameter("id"));
		if (id != null) {
			dataDao.delete(id);
		}
		return "qt-project-data";
	}
	
	@GetMapping("qt-data-success")
	public String success() throws ParseException {
		return "success";
	}
	
	/**
	 * 上传项目资料
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@PostMapping("qt-data-upload")
	@ResponseBody
	public void uploadData(@Valid ProjectDataEntity projectData,Model model, HttpServletRequest request,HttpServletResponse response, 
			@SessionAttribute("user") UserEntity user,
			@RequestParam("file")MultipartFile file) throws ParseException, ServletException, IOException {
		Long projectId=Long.parseLong(request.getParameter("id"));
		ImageEntity img = null;
		projectData=new ProjectDataEntity();
		System.out.println("11111："+projectData);
		try {
			img = imgService.upload(user, "data", file,"");
			System.out.println("图片id："+img.getImageId());
			projectData.setImageId(img.getImageId());
			projectData.setProjectId(projectId);
			dataDao.save(projectData);
			System.out.println("22222："+projectData);
		} catch (Exception e) {
			//obj.put("上传失败！", e);
			System.out.println("上传失败！"+e);
		}
		response.sendRedirect("qt-data-success");
		//request.getRequestDispatcher("qt-team-list").forward(request, response);
	}
	
}
